package org.bminfo.migle.format;

import java.util.Map;

public class SNP {
	
	public String[] snpParser(String content) {
		
//		Iupac iu = new Iupac();

		String[] contentArr = content.split("\n");
		String[] arr = null;
		String line = "";
		String head = "";
		String rs = "";
		String snp = "";
		String snpclass = "";
		String seq = "";
		String iupac_char = "";

		StringBuilder temp_seq = null; // sequence
		StringBuilder fasta = new StringBuilder(); // sequence

		int site = 0;
		int length = 0;
		
		String temp = "";

		for (int i = 0; i < contentArr.length; i++) {
			line = contentArr[i];
			if (line.trim().length() > 0) {
				
				if(line.startsWith(">")){
					//	>gnl|dbSNP|rs5186|allelePos=301|totalLen=601|taxid=9606|snpclass=1|alleles='A/C'|mol=Genomic|build=130
					arr = line.split("\\|");
					head = line;
					
					for (int j = 0; j < arr.length; j++) {
						temp = arr[j].trim();
						if (temp.toLowerCase().startsWith("rs")) {
							rs = temp;
						} else if (temp.startsWith("allelePos")) {
							site = Integer.parseInt(temp.replace("allelePos=", ""));
						} else if (temp.startsWith("totalLen")) {
							length = Integer.parseInt(temp.replace("totalLen=", ""));
						} else if (temp.startsWith("snpclass")) {
							snpclass = temp.replace("snpclasss=", "");
						}else if (temp.startsWith("alleles")) {
							snp = temp.replace("alleles=", "").replace("\'", "");
						}
					}
					
					temp_seq = new StringBuilder();
					continue;
					
				}else{
					temp_seq.append(line.replace(" ", "").replace("\r",""));
				}
			}// end if line > 0
		}// end for

		seq = temp_seq.toString().toUpperCase();
		iupac_char = seq.substring(site-1,site);
		
		
//		>gnl|dbSNP|rs3783553|allelePos=256|totalLen=511|taxid=9606|snpclass=2|alleles='-/TTCA'|mol=Genomic|build=129

//		System.out.println(seq.length());		
		
		for (int i = 0; i < seq.length(); i++) {
			fasta.append(seq.charAt(i));
			if ((i + 1) % 60 == 0) {
				fasta.append("\n");
			}
		}
		
		arr = new String[7];

		arr[0] = head;
		arr[1] = seq;
		arr[2] = Integer.toString(length);
		arr[3] = Integer.toString(site);
		arr[4] = iupac_char;
		arr[5] = snpclass;
		arr[6] = snp;
		
//		System.out.println(arr[0]);
//		System.out.println(arr[1]);
//		System.out.println(arr[2]);
//		System.out.println(arr[3]);

		return arr;
	}
}
